package com.nbjtjc.safe.job.api;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.job.mapper.ScheduleJobMapper;
import com.nbjtjc.safe.job.model.ScheduleJobEntity;
import com.nbjtjc.safe.job.service.ScheduleBusinessJobService;
import com.nbjtjc.safe.job.service.ScheduleJobService;
import com.nbjtjc.safe.job.task.SendWorkBriefTask;
import com.nbjtjc.safe.job.utils.ScheduleUtils;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicMapper;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.safe.util.DateUtils;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.tomcat.util.bcel.Const;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/7/26 10:32
 * 定时业务任务
 **/
@RestController
@CrossOrigin("*")
@RequestMapping("/sys/scheduleBusiness")
public class ScheduleBusinessJobController {

    @Autowired
    private ScheduleJobService scheduleJobService;
    @Autowired
    private ScheduleJobMapper scheduleJobMapper;

    @Autowired
    private ScheduleBusinessJobService scheduleBusinessJobService;
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private EmgPlanPublicMapper emgPlanPublicMapper;

    @PostMapping("/sendWorkBriefTask")
    public Result sendWorkBriefTask(int workBriefId, Date sendDate) {
        ScheduleJobEntity scheduleJobEntity = new ScheduleJobEntity();
        scheduleJobEntity.setCreateTime(new Date());
        scheduleJobEntity.setParams(workBriefId + "");
        scheduleJobEntity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
        scheduleJobEntity.setMethodName("sendWorkBrief");
        scheduleJobEntity.setBeanName("workBriefTask");
        scheduleJobEntity.setBusinessId(workBriefId);
        scheduleJobEntity.setIdType(Constant.ScheduleJobIdType.WORKBRIEF.getValue());
        scheduleJobEntity.setJobTriggerType(Constant.ScheduleJobTriggerType.SIMPLETRIGGER.getValue());
        scheduleJobEntity.setJobTime(sendDate);
        scheduleJobMapper.insertSelective(scheduleJobEntity);
        ScheduleUtils.createScheduleJob(scheduler, scheduleJobEntity, sendDate, 2, 5);
        return Result.success(scheduleJobEntity);
    }

    @PostMapping("/sendWorkBriefAllTask")
    public Result sendWorkBriefAllTask(int startPlanPublicId) {
        Result result = new Result();
        EmgPlanPublic emgPlanPublic = emgPlanPublicMapper.getRunningEmgPlanPublic(startPlanPublicId);
        Date now = new Date();
        if (ObjectUtil.isNotNull(emgPlanPublic)) {

            ScheduleJobEntity jobEntity=new ScheduleJobEntity();
            jobEntity.setParams(startPlanPublicId+"-"+MyShiroUtils.getCompanyId());

            List<ScheduleJobEntity> scheduleJobEntities= scheduleJobMapper.search(jobEntity);
            if(CollectionUtils.isNotEmpty(scheduleJobEntities)){
                result.setFalse("该公司的简报自动生成已经开启，请重新加载数据");
                return result;
            }


            Date publicTime = emgPlanPublic.getSignDate() == null
                    ? emgPlanPublic.getCreateDate() : emgPlanPublic.getSignDate();
            int hours = emgPlanPublic.getTimeInterval();
            List<Date> dateRange= DateUtils.getEmgSendDate(hours,publicTime);
            Date startDate=dateRange.get(0);
            Date endDate=dateRange.get(1);
            ScheduleJobEntity scheduleJobEntity = new ScheduleJobEntity();
            scheduleJobEntity.setCreateTime(new Date());
            scheduleJobEntity.setParams(startPlanPublicId + "-"+MyShiroUtils.getCompanyId()+"-" + MyShiroUtils.getSessionUserId());
            scheduleJobEntity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
            scheduleJobEntity.setMethodName("sendOkWorkBrief");
            scheduleJobEntity.setBeanName("workBriefAllScheduleTask");
            scheduleJobEntity.setBusinessId(startPlanPublicId);
            scheduleJobEntity.setIdType(Constant.ScheduleJobIdType.WORKBRIEF.getValue());
            scheduleJobEntity.setJobTriggerType(Constant.ScheduleJobTriggerType.SIMPLETRIGGER.getValue());
            scheduleJobEntity.setJobTime(endDate);
            scheduleJobMapper.insertSelective(scheduleJobEntity);
            ScheduleUtils.createScheduleJob(scheduler, scheduleJobEntity
                    , endDate, 0, hours *3600);
            result.setData(scheduleJobEntity);
            return result;
        } else {
            result.setFalse("没有该");
            return result;
        }
    }

    @PostMapping("/updateSendWorkBriefTask")
    public Result updateSendWorkBriefDate(Date sendDate, int jobId) throws SchedulerException {
        Result result = scheduleBusinessJobService.updateWorkBriefSendTask(jobId, sendDate);
        return result;
    }


    @PostMapping("/deleteSendWorkBriefTask")
    public Result deleteSendWorkBriefTask(ScheduleJobEntity scheduleJobEntity) throws SchedulerException {
        Result result = scheduleBusinessJobService.deleteSendWorkBriefTask(scheduleJobEntity);
        return result;
    }
}
